package com.akina.matchsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.akina.matchsystem.common.BaseResponse;
import com.akina.matchsystem.common.ErrorCode;
import com.akina.matchsystem.exception.BusinessException;
import com.akina.matchsystem.model.domain.User;
import com.akina.matchsystem.model.request.PrivateChatRequest;
import com.akina.matchsystem.model.request.TeamChatRequest;
import com.akina.matchsystem.model.vo.MessageVO;
import com.akina.matchsystem.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.akina.matchsystem.model.domain.Chat;
import com.akina.matchsystem.mapper.ChatMapper;
import com.akina.matchsystem.service.ChatService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
* @author 浪子心声
* @description 针对表【chat】的数据库操作Service实现
* @createDate 2024-03-31 09:52:23
*/
@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat>
    implements ChatService{

    @Resource
    UserService userService;

    /**
     * 保存群聊信息
     * @param chat
     * @return
     */
    @Override
    public Boolean saveMessage(Chat chat) {
        //校验参数
        if(chat==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        String content = chat.getContent();

        //Date createTime = chat.getCreateTime();
        Long fromId = chat.getFromId();
        if(chat.getChatType()==2){
            Long teamId = chat.getTeamId();
        if((teamId<=0||fromId<=0)||(StringUtils.isAnyBlank(content))){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        } else if (chat.getChatType() == 1) {
            Long toId = chat.getToId();
            if((toId<=0||fromId<=0)||(StringUtils.isAnyBlank(content))){
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        //Chat chat = BeanUtil.copyProperties(messageVO, Chat.class);
        //数据库存储
        return  this.save(chat);
    }

    @Override
    public List<MessageVO> getTeamChat(TeamChatRequest teamChatRequest) {
        //校验参数
        if(teamChatRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long teamId = teamChatRequest.getTeamId();
        if((teamId<=0)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        QueryWrapper<Chat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teamId", teamId);
        queryWrapper.eq("chatType", 2);
        List<Chat> list = list(queryWrapper);

        List<MessageVO> messageVOS = list.stream().map(e -> BeanUtil.copyProperties(e, MessageVO.class)).collect(Collectors.toList());
        //这个把用户名设置了一下
        messageVOS.forEach(e->{
            User user = userService.getById(e.getFromId());
            if(user == null){
                e.setUsername("momo");
            }else{
            e.setUsername(user.getUsername());
            }
        });
        return messageVOS;
    }

    @Override
    public List<MessageVO> getChat(PrivateChatRequest privateChatRequest,User loginUser) {
        //校验参数
        if(privateChatRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long fromId = Long.parseLong(privateChatRequest.getFromId());
        if((fromId<=0)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long loginUserId = loginUser.getId();
/*        LambdaQueryWrapper<Chat> chatLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chatLambdaQueryWrapper.
                and(privateChat -> privateChat.eq(Chat::getFromId, loginUser.getId()).eq(Chat::getToId, toId)
                        .or().
                        eq(Chat::getToId, loginUser.getId()).eq(Chat::getFromId, toId)
                ).eq(Chat::getChatType, chatType);*/

        //.and("toId", fromId);
        //私聊。
        QueryWrapper<Chat> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.eq("chatType", 1).and(
                new Consumer<QueryWrapper<Chat>>() {
                    @Override
                    public void accept(QueryWrapper<Chat> chatQueryWrapper) {
                        chatQueryWrapper.eq("fromId", fromId)
                                .eq("toId",loginUserId).or()
                                .eq("toId", fromId)
                                .eq("fromId", loginUserId);
                    }
                }
        );

        List<Chat> list = list(chatQueryWrapper);

        List<MessageVO> messageVOS = list.stream().map(e -> BeanUtil.copyProperties(e, MessageVO.class)).collect(Collectors.toList());
        //这个把用户名设置了一下
        //这里返回消息的发送id就够了
        messageVOS.forEach(e->{
            User user = userService.getById(e.getFromId());
            if(user == null){
                e.setUsername("momo");
            }else{
                e.setUsername(user.getUsername());
            }
        });
        return messageVOS;
    }


}




